<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public function index(){
		if($this->session->userdata('user_id') == '' || $this->session->userdata('user_name') == ''){
			$this->load->helper(array('url'));
			redirect('/useraz/login', 'location', 301);
		}
		else{
			$this->layout->setTitle('User | Manage');
			$page = $this->uri->segment(3) > 1 ? $this->uri->segment(3) : 1;
			$offset = ($page - 1) * PAGE_ITEM;
			if(isset($_GET['s'])){
				$strFind = quotes_to_entities($_GET['s']);
				$filter = " AND user_name LIKE '%". $strFind ."%' OR user_email LIKE '%". $strFind ."%'";
				$data['find'] = $strFind;
			}
			$sqlUser = "SELECT SQL_CALC_FOUND_ROWS * FROM az_users WHERE 1=1 $filter ORDER BY user_id DESC LIMIT $offset, ".PAGE_ITEM;
			$queryUser = $this->db->query($sqlUser);
			
			// get total
			$sqlTotal = "SELECT FOUND_ROWS() AS found_row";
			$queryTotal = $this->db->query($sqlTotal);
			$dataTotal = $queryTotal->row();
			if($dataTotal->found_row > 0){
				if($dataTotal->found_row > PAGE_ITEM){
					$this->load->library('pagination');
					$config['base_url'] = $this->config->item('base_url').'/user/index/';
					$config['total_rows'] = $dataTotal->found_row;
					$config['per_page'] = PAGE_ITEM;
					$config['num_tag_open'] = '<li>';
					$config['num_tag_close'] = '</li>';
					$config['cur_tag_open'] = '<li><span>';
					$config['cur_tag_close'] = '</span></li>';
					$config['prev_link'] = 'Previous';
					$config['prev_tag_open'] = '<li>';
					$config['prev_tag_close'] = '</li>';
					$config['next_link'] = 'Next';
					$config['next_tag_open'] = '<li>';
					$config['next_tag_close'] = '</li>';
					$config['uri_segment'] = 3;
					$config['first_tag_open'] = '<li>';
					$config['first_tag_close'] = '</li>';
					$config['last_tag_open'] = '<li>';
					$config['last_tag_close'] = '</li>';
					
					$this->pagination->initialize($config);
					$data['paginate'] = $this->pagination->create_links();
				}
				$data['total'] = $dataTotal->found_row;			
				if ($queryUser->num_rows() > 0){
					foreach ($queryUser->result_array() as $row){
						$data['items'][] = $row;
					}
				}
			}
			
			
			$this->layout->view(THEME_DIR.'/home_index', $data);
		}
	}
}